package com.example.study.controller;

import com.example.study.SpringUtils;
import com.example.study.service.ICarService;
import org.junit.Test;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Method;
public class CarTestController {
    @Test
    public void test() {
        Class<?> clazz = ICarService.class;
        String beanName = "carServiceImpl";
        String methodName = "queryCarName";

        // 获取service
        Object service = SpringUtils.getBean(beanName);

        // 测试-无参数传入
        Method method1 = ReflectionUtils.findMethod(clazz, methodName);
        Object result1 = ReflectionUtils.invokeMethod(method1, service);

        // 测试-1个参数传入
        String param1 = "我是参数1";
        Method method2 = ReflectionUtils.findMethod(clazz, methodName, param1.getClass());
        Object result2 = ReflectionUtils.invokeMethod(method2, service, param1);

        // 测试-2个参数传入
        String param2 = "我是参数2";
        String param3 = "我是参数3";
        Method method3 = ReflectionUtils.findMethod(clazz, methodName, param2.getClass(), param3.getClass());
        Object result3 = ReflectionUtils.invokeMethod(method3, service, param2, param3);

        System.out.println("result1:" + result1);
        System.out.println("result2:" + result2);
        System.out.println("result3:" + result3);
    }
}
